import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author zyj
 * @date 2022年06月10日 11:11
 */
public class KFC {
    private List<Food> list = new ArrayList<>();
    String[] names = {"炸鸡","薯条","可乐","汉堡","鸡翅","全家桶"};
    Random r = new Random();
    /**
     * 负责生产食物
     */
    public synchronized void prod(){

        //生产到一定数量以后不应该生产
        if (list.size() == 20){
            try {
                System.out.println(Thread.currentThread().getName() + "食物充足，不需要生产");
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Food f = new Food(names[r.nextInt(names.length)]);
        list.add(f);
        System.out.println(Thread.currentThread().getName() + "生产了" + f.getName());
        notifyAll();

    }

    /**
     * 负责消费
     */
    public synchronized void cus(){
        if (list.size() == 0){
            try {
                System.out.println(Thread.currentThread().getName() + "食物不够，需要生产");
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //消费
        Food f = list.remove(r.nextInt(list.size()));
        System.out.println(Thread.currentThread().getName() + "消费了" + f.getName());

        notifyAll();
    }
}
